package com.michaelbulava.TFS.ui.controls.workspaces;

import com.intellij.ui.AddEditRemovePanel;
import com.intellij.util.EventDispatcher;
import com.michaelbulava.TFS.Messages;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.WorkingFolderType;
import org.jetbrains.annotations.Nullable;

import javax.swing.event.ChangeListener;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by mbulava on 7/16/2015.
 */
public class WorkingFolderDataTable extends AddEditRemovePanel<WorkingFolderData> {

    public static final String COLUMN_TYPE = "type";
    public static final String COLUMN_SERVER = "server";
    public static final String COLUMN_LOCAL = "local";

    private static final String TYPE_ACTIVE = Messages.getString("WorkingFolderDataTable.TypeActive");
    private static final String TYPE_CLOAKED = Messages.getString("WorkingFolderDataTable.TypeCloaked");

    private final EventDispatcher<ChangeListener> myEventDispatcher = EventDispatcher.create(ChangeListener.class);

    public WorkingFolderDataTable(){
        super(new WorkingFolderDataModel(), new ArrayList<WorkingFolderData>(),
                Messages.getString("WorkspaceEditControl.WorkingFoldersLabelText") != null ?
                        Messages.getString("WorkspaceEditControl.WorkingFoldersLabelText") : "Working Folders");
        super.getTable().setShowColumns(true);
    }

    public void addChangeListener(ChangeListener value){
        myEventDispatcher.addListener(value);
    }


    public void setWorkingFolderDataCollection(WorkingFolderDataCollection workingFolderDataCollection){
        super.setData(Arrays.asList(workingFolderDataCollection.getWorkingFolderData()));
    }

    public WorkingFolderDataCollection getWorkingFolderDataCollection(){
        WorkingFolderDataCollection collection = new WorkingFolderDataCollection();
        WorkingFolderData[] folders = getData().toArray(new WorkingFolderData[0]);
        for(int i = 0; i < folders.length; i++){
            collection.add(folders[i]);
        }
        return collection;
    }

    @Nullable
    @Override
    protected WorkingFolderData addItem() {
        Messages.showErrorDialog(this, "Add code here?", "Working on it");
        return null;
    }

    @Override
    protected boolean removeItem(WorkingFolderData workingFolderData) {
        Messages.showErrorDialog(this, "Add code here?", "Working on it");
        return false;
        //TODO add this call to add remve and edit once we get those working.
        //myEventDispatcher.getMulticaster().stateChanged(new ChangeEvent(this));
    }

    @Nullable
    @Override
    protected WorkingFolderData editItem(WorkingFolderData workingFolderData) {
        Messages.showErrorDialog(this, "Add code here?", "Working on it");
        return null;
    }



    private static class WorkingFolderDataModel extends TableModel<WorkingFolderData>{

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Nullable
        @Override
        public String getColumnName(int i) {
            switch(i){
                case 0:
                    return COLUMN_TYPE;
                case 1:
                    return COLUMN_SERVER;
                case 2:
                    return COLUMN_LOCAL;
                default:
                    return null;
            }
        }

        @Override
        public Object getField(WorkingFolderData workingFolderData, int i) {
            switch(i){
                case 0:
                    WorkingFolderType type = workingFolderData.getType();
                    if (WorkingFolderType.MAP.equals(type))
                        return TYPE_ACTIVE;
                    return TYPE_CLOAKED;
                case 1:
                    return workingFolderData.getServerItem();
                case 2:
                    return workingFolderData.getLocalItem();
                default:
                    return  null;
            }
        }
    }
}
